package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.zjitc.entity.Admin;
import net.zjitc.service.AdminService;
import net.zjitc.mapper.AdminMapper;
import net.zjitc.utils.JwtHelper;
import net.zjitc.utils.MD5Util;
import net.zjitc.utils.Result;
import net.zjitc.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author win10
 * @description 针对表【vueshop_admin】的数据库操作Service实现
 * @createDate 2024-12-20 09:57:13
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
        implements AdminService{

    @Autowired
    private JwtHelper jwtHelper;



    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result login(Admin admin) {
        //创建查询器
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername,admin.getUsername());

        //从数据库拿用户
        Admin loginAdmin = adminMapper.selectOne(wrapper);

        //判断一下有没有这个人
        //如果没有，返回用户名错误
        if(loginAdmin==null){
            return Result.build(null,ResultCodeEnum.USERNAME_ERROR);
        }

        //判断一下密码对不对
        if(loginAdmin.getPassword().equals(MD5Util.encrypt(admin.getPassword()))){
            String token = jwtHelper.createToken(Long.valueOf(loginAdmin.getId()));
            Map map = new HashMap<>();
            map.put("token",token);
            return Result.ok(map);
        }
        System.out.println("password:"+MD5Util.encrypt(admin.getPassword()));
        return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
    }

    @Override
    public Result getAdminInfo(long adminId) {
        Admin admin = adminMapper.selectById(adminId);
        if(admin!=null){
            //获取到之后再把密码填空
            admin.setPassword(null);
            Map map=new HashMap<>();
            map.put("loginAdmin",admin);
            return Result.build(admin,200,"获取管理员信息成功");
        }
        return Result.build(null,ResultCodeEnum.NOTLOGIN);
    }

    @Override
    public Result updatePassword(long adminId, String password) {
        Admin admin = adminMapper.selectById(adminId);
        if(admin!=null){
            admin.setPassword(MD5Util.encrypt(password));
            System.out.println("Password:"+password);
            adminMapper.updateById(admin);
            return Result.build(null,200,"修改密码成功");
        }
        return Result.build(null,ResultCodeEnum.NOTLOGIN);
    }

    @Override
    public Result updateAvatar(long adminId, String avatar) {
        Admin admin = adminMapper.selectById(adminId);
        if(admin!=null){
            admin.setAvatar(avatar);
            adminMapper.updateById(admin);
            return Result.build(null,200,"修改头像成功");
        }
        return Result.build(null,ResultCodeEnum.NOTLOGIN);
    }
}